package querylog.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.security.auth.login.LoginException;

import querylog.bean.LogBody;
import querylog.bean.LogHead;
import querylog.bean.PageBean;
import querylog.dao.LogDAO;
import querylog.dao.LogDAOImpl;
import querylog.util.AppUtil;

/**
 * @typeName LogService
 * @Description: 交易日志服务层
 * @author wangxy
 * @date 2017年11月30日下午2:40:06
 * @version v1.0
 */
public class LogService {
	LogDAO logdao = new LogDAOImpl();

	/**
	 * @Title queryLogHeadByCondition
	 * @Description 根据条件查询符合要求的日志头
	 * @param @param sql
	 * @param @return
	 */
	public PageBean queryLogHeadByCondition(String condition,int currentPage,int pageSize) {
		try {
			int recordCount =logdao.count(condition); 	//总记录数
			int totalPage = (int)Math.ceil(recordCount*1.0/pageSize);		//总页数
			List<LogHead> logheads=logdao.getLogHeads(condition, currentPage, pageSize);
			
			PageBean pb = new PageBean();		//分页对象，存放一页日志头
			pb.setLogHeads(logheads);
			pb.setCount(recordCount);
			pb.setCurrentPage(currentPage);
			pb.setPageSize(pageSize);
			pb.setTotalPage(totalPage);
			pb.setCondition(condition);
			
			
			return pb;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @Title queryLogHead
	 * @Description 根据流水号查询日志概要信息
	 * @param @param seq
	 * @param @return
	 */
	public LogHead queryLogHead(String seq) {
		try {
			LogHead head = logdao.getLogHead(seq);
			//将报文xml字符串格式化
			head.setReqinfo(AppUtil.formatXML(head.getReqinfo()));
			head.setRespinfo(AppUtil.formatXML(head.getRespinfo()));
			return head;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @Title queryLogBody
	 * @Description 根据流水号查询日志详细信息
	 * @param @param seq
	 * @param @return
	 */
	public List<List<String>> queryLogBody(String seq) {
		try {
			List<LogBody> bodyList =  logdao.listLogBodys(seq);
			
			List<List<String>> body = new ArrayList<List<String>>();
			for (LogBody logBody : bodyList) {
			  List<String> loginfoParts	 =  AppUtil.textSpilt(logBody.getLoginfo());
			  body.add(loginfoParts);
			}
			
			return body;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	
	/**
	 * @Title generateConditionSql
	 * @Description 根据页面填写情况生成sql语句
	 * @param @param seqNo
	 * @param @param tellerNo
	 * @param @param indate
	 * @param @param hostid
	 * @param @return
	 */
	public String generateConditionSql(String seqNo, String tellerNo,
			String indate, String hostid,String tranCode) {
		StringBuffer sql = new StringBuffer(
				"where 1=1");
		if (!(seqNo == null || seqNo.length() <= 0)) {
			sql.append(" and seqno = '" + seqNo + "'");
		}
		if (!(tellerNo == null || tellerNo.length() <= 0)) {
			sql.append(" and userid = '" + tellerNo + "'");
		}
		if (!(indate == null || indate.length() <= 0)) {
			sql.append(" and indate = '" + indate + "'");
		}
		if (!(hostid == null || hostid.length() <= 0)) {
			sql.append(" and hostid = '" + hostid + "'");
		}
		if (!(tranCode == null || tranCode.length() <= 0)) {
			sql.append(" and trancode = '" + tranCode + "'");
		}
		//按日期倒序排列
		sql.append(" order by indate desc,intime desc");
		String rightSql = sql + "";

		//System.out.println(rightSql);

		return rightSql;
	}

}
